﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Sundry.Common
{
    public class RegexHelper
    {
        #region 正则数据验证
        /// <summary>
        /// 检查字符串是否是时间格式
        /// </summary>.以下正确的输入格式： [2007-9-29], [2007-09-29 10:29:39 pm], [2007/12/31],[2008-10-27 01:21:20.000]
        /// <param name="timeString">时间字符串</param>
        /// <returns></returns>
        public static bool IsTime(string timeString)
        {
            string timeRegex = @"^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])(.\d{3})?\s))([AM|PM|am|pm]{2,2})))?$";
            Regex reg = new Regex(timeRegex);
            Match m = reg.Match(timeString);
            return m.Success;
        }

        /*****************************************************************************************/
        /// <summary>
        /// 是否为数字判断
        /// </summary>
        /// <param name="value">数字字符串</param>
        /// <returns>true或false</returns>
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[+-]?\d*[.]?\d*$");
        }
        /*****************************************************************************************/

        /// <summary>
        /// 判断是否为邮政编码
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static bool IsPostCode(string value)
        {
            return Regex.IsMatch(value, @"\d{6}");
        }

        /// <summary>
        /// 判断是否为电话号码
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static bool IsTel(string value)
        {
            return Regex.IsMatch(value, @"^(\d{3,4})-(\d{7,8})$");
        }

        /// <summary>
        /// 判断是否为手机号码
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static bool IsPhone(string value)
        {
            return Regex.IsMatch(value, @"^1[3,5,7,8]{1}[0-9]{1}[0-9]{8}$");
        }

        /// <summary>
        /// 判断是否为QQ号码
        /// </summary>
        /// <param name="value">字符串</param>
        /// <returns></returns>
        public static bool IsQQ(string value)
        {
            return Regex.IsMatch(value, @"^\d{5,9}$");
        }

        /// <summary>
        /// 检测是否是正确的Url
        /// </summary>
        /// <param name="strUrl">要验证的Url</param>
        /// <returns>判断结果</returns>
        public static bool IsURL(string strUrl)
        {
            return Regex.IsMatch(strUrl, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
        }
        /*****************************************************************************************/
        /// <summary>
        /// 正则替换
        /// </summary>
        /// <returns></returns>
        public static string ReplaceMatch(string strInput, string strRegexOld, string strRegexNew)
        {
            return Regex.Replace(strInput, strRegexOld, strRegexNew);
        }
        /*****************************************************************************************/
        /// <summary>
        /// 获取路径中文件名称
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetFileName(string FullName)
        {
            Regex re = new Regex(@"\\[^\\]*$");
            Match ma = re.Match(FullName);
            if (ma.Success)
                return ma.Value.Substring(1);
            else
                return "";
        }
        #endregion

        #region 正则获取
        /*****************************************************************************************/
        /// <summary>
        /// 获取一个整形随机数
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static int GetRandomNum(int minNum, int maxNum)
        {
            Random r = new Random();
            return r.Next(minNum, maxNum);
        }

        /*****************************************************************************************/
        /// <summary>
        /// 匹配一个组的数据
        /// </summary>
        public static MatchCollection MC(string input, string pattern)
        {
            Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            return regex.Matches(input);
        }

        /// <summary>
        /// 匹配一个组的数据
        /// </summary>
        public static List<string> MCList(string input, string pattern)
        {
            Regex regex = new Regex(pattern, RegexOptions.Singleline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
            MatchCollection mc = regex.Matches(input);
            List<string> mcList = new List<string>();
            for (int i = 0; i < mc.Count; i++)
            {
                if (mc[i].Success)
                {
                    string mcvalue = mc[i].Value.Trim();
                    if (mcvalue.Length > 0)
                    {
                        mcList.Add(mcvalue);
                    }
                }
            }
            return mcList;
        }

        /*****************************************************************************************/
        /// <summary>
        /// 匹配一个数据
        /// </summary>
        public static string MatchValue(string input, string pattern)
        {
            Regex regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Match m = regex.Match(input);
            if (m.Success)
            {
                return m.Value;
            }
            else
            {
                return "";
            }
        }

        public static string MatchV(string input, string pattern, int i)
        {
            Regex regex = new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
            Match m = regex.Match(input);
            if (m.Success)
            {
                return m.Groups[i].Value;
            }
            else
            {
                return "";
            }
        }

        #endregion

        /// <summary>
        /// 去除文章中的HTML标签、空格、换行等
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static string WithoutNoise(string sValue)
        {
            return Regex.Replace(sValue, "<[^>]+?>|\r\n|\t|\n|", "");
        }
        /// <summary>
        /// 去除TAG中的HTML标签、空格、换行等
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static string TagWithout(string sValue)
        {
            return Regex.Replace(sValue, "<[^>]+?>|\r\n|\t|\n|《|》|“|”", "");
        }

        /// <summary>
        /// 去除文章中的HTML标签、空格、换行等
        /// </summary>
        /// <param name="sValue"></param>
        /// <returns></returns>
        public static string ParseScript(string sValue)
        {
            return Regex.Replace(sValue, "<[^>]+?>|'|www", "");
        }
        //        1. 匹配拼音，格式如： 

        //好久不见。
        //hao2jiu3bu2jian4

        //[a-z]+[0-9]{1}

        //([a-z|A-Z]+)\d*? (单个拼音)

        //2. 匹配汉字：

        //[\u4e00-\u9fa5]

        //3. 匹配数字：0.02,0.05,2.5|0.02,0.05,2.5|0.02,0.05,2.5

        //[0-9]+(\.[0-9]+)[,\|]?

        //4. 匹配：普通话[pu3(0.010,0.073,0.170)tong1(0.170,0.235,0.386)hua4(0.386,0.448,0.650)]：

        //(\S*)\[(\S*\((\d.\d+,?){3}\))+\]

        //5. 匹配：http://192.168.79.76:8080/ttawave/test.wav：

        //\b(?<protocol>https?|ftp|file)://(?<domain>[-A-Z0-9.]+)(?<port>:\d+)?/(?<file>.+)\b

    }
}
